package com.dingpeikeji.quickdeer.modules.wx.member.vo;

import java.io.Serializable;
import java.util.List;

import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Length;

import com.dingpeikeji.quickdeer.core.common.config.Global;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Office;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.Role;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.service.SystemService;
import com.google.common.collect.Lists;

public class RegUser implements Serializable{
	
	private static final long serialVersionUID = 1L;
	private String name;	// 姓名
	private String loginName;// 登录名
	private String password;// 密码
	private String repeatPwd; // 重复密码
	private String mobile;	// 手机
	private String wechatId;//微信
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getLoginName() {
		return loginName;
	}
	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}
	@Length(min=8, max=20, message="密码必须是8-20位数字、字母或符号")
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Pattern(regexp="^[1][3,4,5,7,8][0-9]{9}$", message="手机号格式不合法")
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getWechatId() {
		return wechatId;
	}
	public void setWechatId(String wechatId) {
		this.wechatId = wechatId;
	}
	@Length(min=6, max=100, message="重复密码长度不合法")
	public String getRepeatPwd() {
		return repeatPwd;
	}
	public void setRepeatPwd(String repeatPwd) {
		this.repeatPwd = repeatPwd;
	}
	
	/**
	 * 转换微信端注册用户信息，转换前需要进行一次Validator校验
	 * @return
	 */
	public User toUser() {
		User user = new User();
		// 设置大众用户的所属的机构
		Office company = new Office();
		company.setId(Global.getConfig("wx.front.companyid"));
		Office office = new Office();
		office.setId(Global.getConfig("wx.front.officeid"));
		user.setCompany(company);
		user.setOffice(office);
		
		// 角色数据有效性验证，过滤不在授权内的角色
		List<Role> roleList = Lists.newArrayList();
		Role r = new Role();
		r.setId(Global.getConfig("wx.front.roleid"));
		roleList.add(r);
		user.setRoleList(roleList);
		
		user.setName(this.name);
		user.setLoginName(this.loginName);
		user.setPassword(SystemService.entryptPassword(this.password));
		user.setMobile(this.mobile);
		user.setWechatId(this.wechatId);
		return user;
	}
}
